<?php
/**
 * This file contains browser detector class.
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: config.class.php 107 2010-07-20 14:34:05Z sruttloff@googlemail.com $
 * @link      phpmy
 */

/**
 * browser detector
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class browserdetector
{
    public $UA = "";
    /**
     * Netscape, MSIE, Opera
     *
     * @var string
     */
    public $BROWSER = "Unknown";
    /**
     * in95, Unix, Windows 98, Mac, PPC, etc.
     *
     * @var string
     */
    public $PLATFORM = "Unknown";
    /**
     * MajorVersion.MinorVersion
     *
     * @var string
     */
    public $VERSION = "";
    /**
     * Major Version (before . in version string)
     *
     * @var string
     */
    public $MAJORVER = "";
    /**
     * Minor Version (after . in version string)
     *
     * @var string
     */
    public $MINORVER = "";
    /**
     * default constructor
     *
     * @return browserdetector
     */
    public function __construct()
    {
        $this->UA = getenv("HTTP_USER_AGENT");
        $preparens = "";
        $parens = "";
        $postparens = "";
        $i = strpos($this->UA, "(");
        if ($i >= 0)
        {
            $preparens = trim(substr($this->UA, 0, $i));
            $parensTMP = substr($this->UA, $i +1, strlen($this->UA));
            $j = strpos($parensTMP, ")");
            if ($j >= 0)
            {
                $parens = substr($parensTMP, 0, $j);
                $postparens = trim(substr($parensTMP, $j +1, strlen($parensTMP)));
            }
        }
        else
        {
            $preparens = $this->UA;
        }
        $browVer = $preparens;
        $token = trim(strtok($parens, ";"));
        while ($token)
        {
            if ($token == "compatible")
            {
            }
            elseif (eregi("MSIE", "$token"))
            {
                $browVer = $token;
            }
            elseif (eregi("Opera", "$token"))
            {
                $browVer = $token;
            }
            elseif (eregi("X11", "$token") || eregi("SunOS", "$token") || eregi("Linux", "$token"))
            {
                $this->PLATFORM = "Unix";
            }
            elseif (eregi("Win", "$token"))
            {
                $this->PLATFORM = $token;
            }
            elseif (eregi("Mac", "$token") || eregi("PPC", "$token"))
            {
                $this->PLATFORM = $token;
            }
            $token = strtok(";");
        }
        $msieIndex = strpos($browVer, "MSIE");
        if ($msieIndex >= 0)
        {
            $browVer = substr($browVer, $msieIndex, strlen($browVer));
        }
        $leftover = "";
        if (substr($browVer, 0, strlen("Mozilla")) == "Mozilla")
        {
            $this->BROWSER = "Netscape";
            $leftover = substr($browVer, strlen("Mozilla") + 1, strlen($browVer));
        }
        elseif (substr($browVer, 0, strlen("Lynx")) == "Lynx")
        {
            $this->BROWSER = "Lynx";
            $leftover = substr($browVer, strlen("Lynx") + 1, strlen($browVer));
        }
        elseif (substr($browVer, 0, strlen("MSIE")) == "MSIE")
        {
            $this->BROWSER = "IE";
            $leftover = substr($browVer, strlen("MSIE") + 1, strlen($browVer));
        }
        elseif (substr($browVer, 0, strlen("Microsoft Internet Explorer")) == "Microsoft Internet Explorer")
        {
            $this->BROWSER = "IE";
            $leftover = substr($browVer, strlen("Microsoft Internet Explorer") + 1, strlen($browVer));
        }
        elseif (substr($browVer, 0, strlen("Opera")) == "Opera")
        {
            $this->BROWSER = "Opera";
            $leftover = substr($browVer, strlen("Opera") + 1, strlen($browVer));
        }
        elseif (substr($browVer, 0, strlen("iCab")) == "iCab") {
            $this->BROWSER = "iCab";
            $leftover = substr($browVer, strlen("iCab") + 1, strlen($browVer));
        }
        $leftover = trim($leftover);
        /* CHECK FOR OPERA BROWSERS BELOW VERSION 4.0 */
        if (!empty ($postparens))
        {
            if (substr($postparens, 0, strlen("Opera")) == "Opera")
            {
                $this->BROWSER = "Opera";
                $v = trim(substr($postparens, strlen("Opera") + 1, strlen($postparens)));
                $v = trim(substr($v, 0, strpos($v, " ")));
                $leftover = $v;
            }
        }
        /* CHECK FOR NETSCAPE 6 PREVIEW RELEASES */
        if (!empty ($postparens))
        {
            if (eregi("Netscape6", $postparens))
            {
                $this->BROWSER = "Netscape";
                $i = strpos($postparens, "Netscape6");
                if ($i >= 0)
                {
                    $v = trim(substr($postparens, $i +strlen("Netscape6") + 1, strlen($postparens)));
                    $leftover = $v;
                }
            }
        }
        $i = strpos($leftover, " ");
        if ($i > 0)
            $this->VERSION = substr($leftover, 0, $i);
        else
            $this->VERSION = $leftover;
        $j = strpos($this->VERSION, ".");
        if ($j >= 0)
        {
            $this->MAJORVER = substr($this->VERSION, 0, $j);
            $this->MINORVER = substr($this->VERSION, $j +1, strlen($this->VERSION));
        }
        else
            $this->MAJORVER = $this->VERSION;
    }
}